package ioc

import (
	v1 "go-pioneer/api/v1"
	"go-pioneer/runtime"
	"time"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// InitGinHandler 初始化Gin本身
func InitGinHandler(mdls []gin.HandlerFunc, exampleHandler *v1.ExampleHandler) *gin.Engine {
	server := gin.Default()
	server.Use(mdls...)
	exampleHandler.RegisterRoutes(server)
	return server
}

// InitMiddlewares 初始化Gin引用的中间件信息
func InitMiddlewares() []gin.HandlerFunc {
	return []gin.HandlerFunc{
		cors.New(cors.Config{
			AllowOrigins:     []string{"http://localhost:3000", "http://localhost:3001", "http://10.1.3.118:9527"}, // 允许的源列表，可以使用通配符"*"允许所有来源
			AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},                         // 允许的HTTP方法
			AllowHeaders:     []string{"Origin", "Content-Type", "Authorization", "X-Request-Id"},                  // 允许的请求头
			ExposeHeaders:    []string{"Authorization"},                                                            // 允许返回给前端的ResponseHeader
			AllowCredentials: true,                                                                                 // 如果需要携带Cookie，需要设置为true                                                   // 如果需要携带Cookie，需要设置为true
			MaxAge:           12 * time.Hour,
		}),
		runtime.GinLogger(zap.L()),
	}
}
